﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace hse_2013_local_game2d
{
    public class Plane
    {
        public delegate void PlaneExploaded(Plane p);
        public static event PlaneExploaded PlaneBurn;
        public delegate void TransformNeeded(Graphics g);
        public static event TransformNeeded MoveChords;
        private Point position;
        public Point Position
        {
            get { return position; }
            set { position = value; }
        }
        private Point rightDownPosition;
        public Point RightDownPosition
        {
            get { return rightDownPosition; }
        }
        private Point rightUpPosition;
        public Point RightUpPosition
        {
            get { return rightUpPosition; }
        }
        private int velocity;
        public int Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }
        private double uVelocity;
        public double UVelocity
        {
            get { return uVelocity; }
            set { uVelocity = value; }
        }
        private Image img;
        private Image splashImg;
        private Image burnImg;
        private double angle;
        public double Angle
        {
            get { return angle; }
        }
        private Point[] destinationPoints;
        private bool flightMode = false;

        public bool FlightMode
        {
            get { return flightMode; }
        }
        private bool drawnMode = false;
        public bool DrawnMode
        {
            get { return drawnMode; }
        }
        private bool burnMode = false;

        public bool BurnMode
        {
            get { return burnMode; }
        }
        private bool rideMode =true;
        public bool RideMode
        {
            get { return rideMode; }
        }
        public Plane(Point position, int velocity, double uVelocity,Image img,Image burnImg, Image splashImg)
        {
            this.position = position;
            this.velocity = velocity;
            this.uVelocity = uVelocity;
            this.img = img;
            this.burnImg = burnImg;
            this.splashImg = splashImg;
            angle = 0;
            destinationPoints = new Point[3];
            destinationPoints[0] = position;
            destinationPoints[1] = new Point(position.X + 100,position.Y);
            destinationPoints[2] = new Point(position.X, position.Y + 50);
        }
        public void OnPaint(Graphics G)
        {
            G.DrawImage(img, destinationPoints);
            if (!rideMode)
            {
                MoveChords(G);
            }
        }
        public void Move()
        {
            if (rideMode)
            {
                position.X = position.X + velocity;
                destinationPoints[0] = position;
                destinationPoints[1] = new Point((int)(position.X + 100 * Math.Cos(angle)), (int)(position.Y + 100 * Math.Sin(angle)));
                destinationPoints[2] = new Point((int)(position.X - 50 * Math.Sin(angle)), (int)(position.Y + 50 * Math.Cos(angle)));
            }
            if (flightMode|burnMode)
            {
                position.X = position.X + velocity;
                position.Y = Convert.ToInt32(position.Y + uVelocity);
                if (uVelocity != 0)
                    angle = (Math.Atan(uVelocity / velocity));
                else
                    angle = 0;
                destinationPoints[0] = new Point(250,position.Y);
                destinationPoints[1] = new Point((int)(250 + 100 * Math.Cos(angle)), (int)(position.Y + 100 * Math.Sin(angle)));
                destinationPoints[2] = new Point((int)(250 - 50 * Math.Sin(angle)), (int)(position.Y + 50 * Math.Cos(angle)));
                rightUpPosition = new Point((int)(position.X + 100 * Math.Cos(angle)), (int)(position.Y + 100 * Math.Sin(angle)));
                rightDownPosition = new Point((int)(rightUpPosition.X - 50 * Math.Sin(angle)), (int)(rightUpPosition.Y + 50 * Math.Cos(angle)));
                uVelocity = uVelocity + 0.1;
            }
            if (drawnMode)
            {
                destinationPoints[0] = new Point(225,position.Y-25);
                destinationPoints[1] = new Point((int)(250 + 100 * Math.Cos(angle))+25, (int)(position.Y + 100 * Math.Sin(angle))-25);
                destinationPoints[2] = new Point((int)(250 - 50 * Math.Sin(angle))-25, (int)(position.Y + 50 * Math.Cos(angle)));
            }
        }
        public void FlightModeOn()
        {
            flightMode = true;
            drawnMode = false;
            burnMode = false;
            rideMode = false;
        }
        public void DrawnModeOn()
        {
            flightMode = false;
            drawnMode = true;
            burnMode = false;
            rideMode = false;
            img = splashImg;
        }
        public void BurnModeOn()
        {
            flightMode = false;
            drawnMode = false;
            burnMode = true;
            rideMode = false;
            img = burnImg;
        }
        public void PlaneBurnOccured(Plane p)
        {
            PlaneBurn(p);
        }
    }
}
